package the.lesson.algorithm.combination;

import java.util.ArrayList;
import java.util.List;

/**
 *  6位十六进制有多少组合
 *
 *  转换为树形结构的问题，可以用递归解决
 */
public class App 
{
    public static void main( String[] args ) {
        comb(4);
    }

    private static String[] values = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    private static List<String> comb(int n) {
        List<String> codes = new ArrayList<>();
        comb(codes, "", values.length, n, 0);
        return codes;
    }

    /**
     * 递归
     * @param codes
     * @param code
     * @param len
     * @param n
     * @param start
     */
    private static void comb(List codes, String code, int len, int n, int start) {
        if (n == 0) {
            codes.add(code);
            System.out.println(code);
            return;
        }
        for (int i = start; i < len; i++) {
            code += values[i];
            comb(codes, code, len, n - 1, start);
            code = code.substring(0, code.length() - 1);
        }
    }
}
